package com.tamartech.greendb.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import com.tamartech.greendb.data.Address;

// class to initially import Jonathan's spreadsheet. Unlikely ever to need it again
public class PostalVoterFilter_old2
{
    private static Logger logger = Logger.getLogger (PostalVoterFilter_old2.class);
    
    private static final int COL_REF1 = 0;
    private static final int COL_REF2 = 1;
    private static final int COL_ROW_TYPE = 2;
    private static final int COL_BIRTHDAY_18 = 3;
    private static final int COL_SURNAME = 4;
    private static final int COL_FIRST_NAMES = 5;
    private static final int COL_ADDRESS1 = 6;
    private static final int COL_ADDRESS2 = 7;
    private static final int COL_ADDRESS3 = 8;
    private static final int COL_ADDRESS4 = 9;
    private static final int COL_ADDRESS5 = 10;
    private static final int COL_ADDRESS6 = 11;
    private static final int POSTAL_COL = 22;
    
	public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0] + ".CSV");
        File outFile = new File (args[0] + "-postal2.csv");
	    
        LabeledCSVParser csv = new LabeledCSVParser (new CSVParser (new FileInputStream (inFile)));
        PrintWriter out = new PrintWriter (outFile);
        
        Address prevAddress = new Address(); // i.e. initialise to empty one
        for (;;)
        {
            String[] line = csv.getLine();
            
            if (line == null)
                break;
            
            if (line.length < 23 || !line[POSTAL_COL].trim().equals("Postal"))
                continue;
            
            String[] addressLines = new String [6];
            addressLines[0] = line[COL_ADDRESS1].trim();
            addressLines[1] = line[COL_ADDRESS2].trim();
            addressLines[2] = line[COL_ADDRESS3].trim();
            addressLines[3] = line[COL_ADDRESS4].trim();
            addressLines[4] = line[COL_ADDRESS5].trim();
            addressLines[5] = line[COL_ADDRESS6].trim();
            
            Address address = createAddress (addressLines);
            
            if (address.equals(prevAddress))
                continue;
            
            prevAddress = address;
            
            StringBuffer outLine = new StringBuffer();
            
            outLine.append(line[0] + ",");
            outLine.append(line[1] + ",");
            outLine.append(line[2] + ",");
            outLine.append(line[4] + ",");
            outLine.append(line[5] + ",");
            outLine.append(line[12] + ",");
            outLine.append(line[13] + ",");
            outLine.append(line[14] + ",");
            outLine.append(line[15] + ",");
            outLine.append(line[16] + ",");
            outLine.append(line[21] + ",");
            outLine.append(line[22] + ",");
            
            out.println (outLine.toString());
        }
        
        csv.close();
        out.close();

	}
	
    private static Address createAddress (String[] addressLines)
    {
        Address address = new Address();
        
        // reverse through the address to find the last populated line. That will be the postcode
        
        int postcodeIndex = addressLines.length - 1;
        
        for (;;)
        {
            if (postcodeIndex < 0 || addressLines[postcodeIndex].length() > 0)
                break;
                
            postcodeIndex--;
        }
        
        if (postcodeIndex >= 0)
        {
            address.setPostcode (addressLines[postcodeIndex]);
            
            if (postcodeIndex > 0)
                address.setAddress1(addressLines[0]);
            if (postcodeIndex > 1)
                address.setAddress2(addressLines[1]);
            if (postcodeIndex > 2)
                address.setAddress3(addressLines[2]);
            if (postcodeIndex > 3)
                address.setAddress4(addressLines[3]);
            if (postcodeIndex > 4)
                address.setAddress5(addressLines[4]);           
        }
        
        return address;
    }
	
} 